// areaLeader/pages/leaderGroup/leaderGroup.js
// pages/flag_group/flag_group.js
var common = require("../../../common/common.js").common;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        alert: 0,
        page: 1,
        search_con: "",
        group: []
    },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        var that = this;
        that.setData({
            page: 1,
            group: []
        });
        that.getinfo();
        wx.stopPullDownRefresh();
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        wx.hideShareMenu();
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        that.getinfo();
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getinfo();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        var that = this;
        var leader_id = that.data.leader_id;
        var path = "/pages/group_info/group_info?my_leaderID=" + leader_id;
        return {
            title: "旗下" + that.data.leaderName,
            path: path
        };
    },
    //搜索关键词
    getValue: function(e) {
        var that = this;
        var search_con = e.detail.value;
        that.setData({
            search_con: search_con
        });
    },
    search: function(e) {
        var openid = wx.getStorageSync("user").openid;
        var that = this;
        var search = that.data.search_con;
        if (search == "") {
            wx.showModal({
                title: "提示",
                content: "请输入搜索关键词",
                showCancel: false,
                success: function() {
                    return false;
                }
            });
        }
        common.request({
            endpoint: "Leader/undermember/underleader",
            method: "post",
            data: {
                openid: openid,
                search: search
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                that.setData({
                    group: res.extra
                });
            }
        });
    },
    getinfo: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "leader-area/leader-area-leader",
            method: "post",
            data: {
                page: that.data.page
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                var group = res.extra;
                if (group.length > 0) {
                    group = that.data.page == 1 ? group : that.data.group.concat(group);
                    that.setData({
                        group: group,
                        page: that.data.page + 1
                    });
                }
            }
        });
    },
    //跳转共用生成海报页面
    makeposter: function() {
        wx.navigateTo({
            url: "/pages/share/share"
        });
    },
    callphone: function(e) {
        var phone = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: phone
        });
    },
    alert: function() {
        var alert = this.data.alert;
        if (alert == 0) {
            alert = 1;
        } else {
            alert = 0;
        }
        this.setData({
            alert: alert
        });
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {
                page: that.data.page
            },
            success: res => {
                wx.setNavigationBarTitle({
                    title: "旗下" + (res.extra.leader_title ? res.extra.leader_title : "团长")
                });
                that.setData({
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长"
                });
            }
        });
    },
    goToLeaderFunction(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    }
});